<?php

namespace app\admin\controller;

use app\admin\model\Goods;
use app\admin\service\GoodsCategoryService;
use app\admin\service\GoodsService;
use think\facade\View;
use think\exception\ValidateException;

class GoodsController extends BaseController
{
    public function index()
    {   
        $goodsService = new GoodsService();
        $list = $goodsService->getGoodsList();

        return View::fetch('',['list' => $list]);
    }
    
    public function create()
    {   
        $goodsCategoryService = new GoodsCategoryService();
        $options = $goodsCategoryService->getCategoryOption(true);
        
        return View::fetch('',['options' => $options]);
    }

    public function edit()
    {   
        $id = $this->request->get('id/d');

        $goodsCategoryService = new GoodsCategoryService();
        $options = $goodsCategoryService->getCategoryOption(true);

        $goodsService = new GoodsService();
        $row = $goodsService->findGoods($id);

        return View::fetch('',['options' => $options, 'row' => $row]);
    }

    public function save()
    {
        $param = $this->request->post();

        try {
            $this->validate($param,[
                'category_id' => 'require|integer',
                'sort' => 'require|integer|egt:0',   
                'goods_name' => 'require|max:255',
                'main_image' => 'require',
                'goods_images' => 'require',
                'integral' => 'require|integer',
                'content' => 'require',
                'goods_attr' => 'require',
                'skus' => 'require|array',
                'status' => 'require|in:0,1',
            ],[
                'main_image.require' => '请上传商品主图!',
            ]);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $goodsService = new GoodsService();
        $id =  $goodsService->saveGoods($param);
        
        return $id ? json(['code' => 1, '保存成功!']) : json(['code' => 0, '保存失败!']);

    }

    public function switch()
    {
        $param = $this->request->post();

        try {
            $this->validate($param,[
                'id' => 'require|integer',
                'status' => 'require|in:0,1',
            ]);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        Goods::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }


    public function update()
    {
        $param = $this->request->post();

        try {
            $this->validate($param,[
                'category_id' => 'require|integer',
                'sort' => 'require|integer|egt:0',   
                'goods_name' => 'require|max:255',
                'main_image' => 'require',
                'goods_images' => 'require',
                'integral' => 'require|integer',
                'content' => 'require',
                'skus' => 'require|array',
                'status' => 'require|in:0,1',
            ],[
                'main_image.require' => '请上传商品主图!',
            ]);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $goodsService = new GoodsService();
        $id =  $goodsService->updateGoods($param);
        
        return $id ? json(['code' => 1, '保存成功!']) : json(['code' => 0, '保存失败!']);
    }


    public function delete()
    {
        $id = $this->request->get('id/d');
        $result = Goods::destroy($id);

        return $result ?  json(['code' => 1, 'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败!']);
    }
}